<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 订单类
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   create by allen.qiang
 * @version 1.0
 */

class productionOrder {

    private $dbca;

    public function __construct( $dbca ) {
        $this->dbca = $dbca;
    }


    /**
     * 删除订单
     *
     */
    public function delete ( $arg_array ) {

        try {
            $this->dbca->beginTransaction();

                $sql_del_specgroup_info = 'DELETE FROM bill_alter_notice WHERE notice_id = ? ';

                $this->dbca->stmt_query($sql_del_specgroup_info,'i',$arg_array['notice_id']);

            $this->dbca->commit();

            return array( 'code' => 200, 'message' => '删除成功' );
        } catch (Exception $e) {
            $this->dbca->rollBack();
            error_log($e->getTraceAsString());
            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }
    }

    /**
     * 订单列表
     */
    public function productionOrder ( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();
        
        //通知消息id
        if (isset($arg_array['notice_id']) && !empty($arg_array['notice_id'])) {
            $sql .= ' AND notice_id = ?';
            $params_type .= 'i';
            $params_array[] = trim($arg_array['notice_id']);
        }

        //订单状态
        if (isset($arg_array['bill_status']) && !empty($arg_array['bill_status'])) {
            $sql .= ' AND bill_status = ?';
            $params_type .= 'i';
            $params_array[] = trim($arg_array['bill_status']);
        }

        //城市id
        if (isset($arg_array['cityID']) && !empty($arg_array['cityID'])) {
            $sql .= ' AND city_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['cityID'];
        }

        //配送站id
        if (isset($arg_array['distribution_id']) && !empty($arg_array['distribution_id'])) {
            $sql .= ' AND distribution_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['distribution_id'];
        }

        //订单编号
        if (isset($arg_array['bill_code']) && !empty($arg_array['bill_code'])) {
            $sql .= ' AND bill_code LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['bill_code']) . '%';
        }

        //配送日期
        if (isset($arg_array['send_date']) && !empty($arg_array['send_date'])) {
            $sql .= ' AND send_date = ?';
            $params_type .= 's';
            $params_array[] =$arg_array['send_date'];
        }

        //拓展字段
        if( isset($arg_array['extend']) && !empty($arg_array['extend']) ){
            $sql .= $arg_array['extend'];
        }

        //拓展字段
        if( isset($arg_array['extend2']) && !empty($arg_array['extend2']) ){
            $sql .= $arg_array['extend2'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM bill_alter_notice ' . $sql;

                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY is_handle desc, %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY is_handle desc, %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询订单信息
            $search_sql = 'SELECT * FROM bill_alter_notice ' . $sql;

            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

            	$unitName = '';
            	//mod by yim.hu 20141121 FUN-BK-0035-订单状态优化 --start           	          	
//                 if ($item->bill_status == 1) {
//                     $unitName = '确认';
//                 } elseif ($item->bill_status == 2) {
//             		$unitName = '生产';
//             	} else if ($item->bill_status == 3) {
//             		$unitName = '入库';
//             	} else if ($item->bill_status == 4) {
//             		$unitName = '出库';
//             	}else if ($item->bill_status == 5) {
//                     $unitName = '下车';
//                 }else if ($item->bill_status == 6) {
//                     $unitName = '配送中';
//                 }else if ($item->bill_status == 7) {
//                     $unitName = '完成';
//                 }           	
            	if ($item->bill_status == 1) {
            		$unitName = '已确认';
            	} elseif ($item->bill_status == 2) {
            		$unitName = '已打印生产单';
            	} else if ($item->bill_status == 3) {
            		$unitName = '已质检入库';
            	} else if ($item->bill_status == 4) {
            		$unitName = '已上车';
            	}else if ($item->bill_status == 5) {
            		$unitName = '已下车';
            	}else if ($item->bill_status == 6) {
            		$unitName = '已出仓';//暂时没
            	}else if ($item->bill_status == 7) {
            		$unitName = '已完成';
            	} 
            	//mod by yim.hu 20141121 FUN-BK-0035-订单状态优化 --end            	           	
                $item_array[] = array(
                    'notice_id'	        => $item->notice_id,
                    'bill_code'	        => $item->bill_code,
                    'bill_no'	            => $item->bill_no,
                    'bill_status'	        => $unitName,
                    'distribution_id'   =>$item->distribution_id,
                    'distribution_name'   =>$item->distribution_name,
                    'goods_info'	        => $item->goods_info,
                    'alter_content'	=> $item->alter_content,
                    'send_date'	=> $item->send_date,
                    'is_handle'	=> $item->is_handle

                );
            }
            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
    }

}